#include "MapPathCreator.h"

MapPathCreator::MapPathCreator(MapGraph Graph, int width, int height):m_Width(width), m_Height(height), m_Created(true)
{
	m_pMapGraph = MapGraph::GetGraph(height, width);
}


void MapPathCreator::Init(const MapGraph& Graph, const int &width, const int& height)
{
	m_Created = true;
	m_Width = width;
	m_Height = height;
	m_pMapGraph = MapGraph::GetGraph(height, width);
}

int MapPathCreator::GetClosestNodeToPosition(Vector2D Pos)
{
	if(Pos.x > m_Width * TILE_DIMENSIONS)
		Pos.x = (m_Width - 1) * TILE_DIMENSIONS;
	if(Pos.y > m_Height * TILE_DIMENSIONS)
		Pos.y = (m_Height - 1) * TILE_DIMENSIONS;

	double NodeX = (Pos.x)/32.0;
	double NodeY = (Pos.y)/32.0;
	
	int nodeIndx = int(NodeY + .5)*m_Width + int(NodeX + .5);

	return nodeIndx;
}

Path* MapPathCreator::GeneratePath(Vector2D start, Vector2D end, bool noisy)
{
	assert("MapPathCreator attempting to generate a path with no valid MapGraph" && m_Created);

	int startNode = GetClosestNodeToPosition(start);
	int endNode = GetClosestNodeToPosition(end);

	Path* aPath = new Path(*m_pMapGraph, startNode, endNode, noisy);

	return aPath;
}